package edu.levantai;


import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class CountryList extends ListActivity {

	public static final String ROW_ID = "row_id";
	private ListView conListView;
	private CursorAdapter conAdapter;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

		setContentView(R.layout.main);

		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.window_title);

		// map each name to a TextView
		String[] from = new String[] { "name" };
		int[] to = new int[] { R.id.countryTextView };
		conAdapter = new SimpleCursorAdapter(CountryList.this,
				R.layout.country_list, null, from, to);
		setListAdapter(conAdapter); // set Adapter

		conListView = getListView();
		conListView.setOnItemClickListener(viewConListener);

	}

	private class GetContacts extends AsyncTask<Object, Object, Cursor> {
		DatabaseConnector dbConnector = new DatabaseConnector(CountryList.this);

		@Override
		protected Cursor doInBackground(Object... arg0) {
			dbConnector.open();
			return dbConnector.getAllContacts();
		}

		protected void onPostExecute(Cursor result) {
			conAdapter.changeCursor(result); // set the adapter's Cursor
			dbConnector.close();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		new GetContacts().execute((Object[]) null);
	}

	@Override
	protected void onStop() {
		Cursor cursor = conAdapter.getCursor();

		if (cursor != null)
			cursor.deactivate();

		conAdapter.changeCursor(null);
		super.onStop();
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.country_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = new Intent(CountryList.this, AddEditCountry.class);
		startActivity(intent);
		return super.onOptionsItemSelected(item);
	}

	OnItemClickListener viewConListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Intent intent = new Intent(CountryList.this, ViewCountry.class);
			intent.putExtra(ROW_ID, arg3);
			startActivity(intent);
		}

	};

}
